package com.gen.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.gen.service.ILogService;
import com.gen.service.IPassTableService;
import com.zzx.entity.Goods;
import com.zzx.entity.Log;
import com.zzx.utils.dateUtils;


@Controller
public class PassTableController {
	@Autowired
	private IPassTableService passTableService;
	
	@Autowired
	private ILogService logService;

	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private HttpSession session;


	@RequestMapping("/loginPage.do")
	public String loginPage() {
		return "login";
	}

	/*
	 * result 0 账户被冻结了 result 1 正确--登入系统 产生session result -1 账户或者密码错误
	 */
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public ModelAndView login(@RequestParam("uid") Integer uid, @RequestParam("encodedpass") String encodedpass) {
		Integer result = null;
		ModelAndView modelAndView = new ModelAndView();
		result = passTableService.selectStatusByUidAndPassword(uid, encodedpass);
		if (Integer.valueOf(1).equals(result)) {
			session.setAttribute("uid", uid);
			String ip = request.getRemoteAddr();
			String loginTime = dateUtils.getdate();
			session.setAttribute("ip", ip);
			session.setAttribute("loginTime", loginTime);
			
//			创建购物车
			Map shoppingCart = new HashMap<Integer, Goods>();
			session.setAttribute("shoppingCart", shoppingCart);
			
			modelAndView.setViewName("redirect:goodsList.do");
		} else {
			String msg = " ";
			if (result == null)
				msg = "账户名或密码错误！";
			else if (Integer.valueOf(0).equals(result))
				msg = "账户处于冻结状态";
			modelAndView.addObject("msg", msg);
			modelAndView.setViewName("login");
		}
		return modelAndView;
	}
	
	@RequestMapping("/logOut.do")
	public ModelAndView logOut() throws IOException, ParseException {
		ModelAndView modelAndView = new ModelAndView();
		//当session传入false后只会从内存中检索有没有session，没有就返回空，不创建。
		HttpSession session = request.getSession(false);
		if(session!=null){
			//记录操作
			Integer uid =(int) session.getAttribute("uid");
			String ip = (String) session.getAttribute("ip");
			String loginTime =(String) session.getAttribute("loginTime");
			String logoutTime = dateUtils.getdate();
			String online_time = dateUtils.timedif(loginTime, logoutTime);
			
			Log log = new Log(null, uid, ip, loginTime, null, online_time);
			logService.insertInfoToLog(log);
			
			//移除登陆标记并销毁session
			session.removeAttribute("uid");
			session.invalidate();
		}
		//注销后跳到首页
		modelAndView.setViewName("redirect:loginPage.do");
		return modelAndView;
	}
}
